<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( JApplicationHelper::getPath( 'admin_html' ) );

$task = @JRequest::getCmd('task');
echo $task;
switch($task){
	case 'remove':
		removeItem($option);
		break;
	case 'add':
		createNew($option);
		break;
	case 'edit':
		updateForm($option);
		break;
	case 'save':
		saveUpdate($option);
		break;
	default:
		display($option);
		break;	
}
function display($option){
	global $mainframe;
	$q = "SELECT * FROM #__topbanner ORDER BY `id` ASC";
	$db =& JFactory::getDBO();
	$db->setQuery($q);
	$rows = $db->loadObjectList();
	topbanner_html::display($rows);
}
function removeItem($option){
	global $mainframe;
	//var_dump($_REQUEST);
	$cid = JRequest::getVar("cid",array());
	
	//var_dump($cid);
	if(count($cid)>0){
		$in_phrase = "(";
		for($i=0;$i<count($cid);$i++){
			//$in_phrase .= (($i == 0)||($i == count($cid) - 1))?$cid[$i]:cid[$i].",";
			if($i == count($cid) - 1){
				$in_phrase .= $cid[$i];
			}else{
				$in_phrase .= $cid[$i].",";
			}
		}
		$in_phrase .= ")";
		echo $in_phrase;
		$q = "DELETE FROM #__topbanner WHERE `id` IN ".$in_phrase;
		$db =& JFactory::getDBO();
		$db->setQuery($q);
		$db->query();
		//var_dump($db);
	}
	go2com();
}
function updateForm($option){
	global $mainframe;
	$id = @JRequest::getInt('id');
	$db =& JFactory::getDBO();
	$q = "SELECT * FROM #__topbanner WHERE `id` = $id";
	$db->setQuery($q);
	$row = $db->loadObjectList();
	topbanner_html::updateForm($row);
}
function createNew($option){
	$row = array();
	topbanner_html::updateForm($row);
}
function saveUpdate($option){
	global $mainframe;
	$ha_absolute_path = JPATH_ROOT.DS."images".DS."topbanner".DS;
	$db =& JFactory::getDBO();
	$id = @JRequest::getInt('id');
	//$link = @JRequest::getVar("link");
	$q = "SELECT * FROM #__topbanner WHERE `id` = ".$id;
	$db->setQuery($q);
	$temp = $db->loadObjectList();
	$count = count($temp);
	
	if($_FILES["file_image"]["name"] != ""){
		
		if ((($_FILES["file_image"]["type"] == "image/gif")
			|| ($_FILES["file_image"]["type"] == "image/jpeg")
			|| ($_FILES["file_image"]["type"] == "image/jpg")
			|| ($_FILES["file_image"]["type"] == "image/png")
			|| ($_FILES["file_image"]["type"] == "image/pjpeg"))
			)
			{
				
				if ($_FILES["file_image"]["error"] > 0)
				{
					echo "Return Code: " . $_FILES["file_image"]["error"] . "<br />";
				}
				else
				{
					
					if($_FILES["link"]["name"] != ""){
		
						if ((($_FILES["link"]["type"] == "image/gif")
							|| ($_FILES["link"]["type"] == "image/jpeg")
							|| ($_FILES["link"]["type"] == "image/jpg")
							|| ($_FILES["link"]["type"] == "image/png")
							|| ($_FILES["link"]["type"] == "image/pjpeg"))
							)
							{
								
								if ($_FILES["link"]["error"] > 0)
								{
									echo "Return Code: " . $_FILES["link"]["error"] . "<br />";
								}
								else
								{
									
									$surfix_string = uniqid();
									//echo "<script>alert('".$_FILES["file_image"]["tmp_name"]."');</script>";
									@move_uploaded_file($_FILES["file_image"]["tmp_name"],
									$ha_absolute_path .$surfix_string."_".$_FILES["file_image"]["name"]);
									$image_name = $surfix_string."_".$_FILES["file_image"]["name"];
									
									$surfix_string = uniqid();
									//echo "<script>alert('".$_FILES["file_image"]["tmp_name"]."');</script>";
									@move_uploaded_file($_FILES["link"]["tmp_name"],
									$ha_absolute_path .$surfix_string."_".$_FILES["link"]["name"]);
									$link = $surfix_string."_".$_FILES["link"]["name"];
									
									
									if($count > 0)
									{
										$db =& JFactory::getDBO();
										$q = "UPDATE #__topbanner SET `image` = '$image_name', `link` = '$link' WHERE `id` = $id";
										$db->setQuery($q);
										$db->query();
									}else{
										$db =& JFactory::getDBO();
										$q = "INSERT INTO #__topbanner(`menuid`,`link`,`image`) VALUES('$id','$link','$image_name')";
										$db->setQuery($q);
										$db->query();
									}
								}
							}
							else{
								echo "Invalid file";
							}
					}
					else{
						$surfix_string = uniqid();
						//echo "<script>alert('".$_FILES["file_image"]["tmp_name"]."');</script>";
						@move_uploaded_file($_FILES["file_image"]["tmp_name"],
						$ha_absolute_path .$surfix_string."_".$_FILES["file_image"]["name"]);
						$image_name = $surfix_string."_".$_FILES["file_image"]["name"];
						
						/*$surfix_string = uniqid();
						//echo "<script>alert('".$_FILES["file_image"]["tmp_name"]."');</script>";
						@move_uploaded_file($_FILES["link"]["tmp_name"],
						$ha_absolute_path .$surfix_string."_".$_FILES["link"]["name"]);
						$link = $surfix_string."_".$_FILES["link"]["name"];*/
						
						
						if($count > 0)
						{
							$db =& JFactory::getDBO();
							$q = "UPDATE #__topbanner SET `image` = '$image_name' WHERE `id` = $id";
							$db->setQuery($q);
							$db->query();
						}else{
							$db =& JFactory::getDBO();
							$q = "INSERT INTO #__topbanner(`menuid`,`image`) VALUES('$id','$image_name')";
							$db->setQuery($q);
							$db->query();
						}
						
					}
					
				}
			}
			else{
				echo "Invalid file";
			}
	}
	else{
		if($_FILES["link"]["name"] != ""){
		
			if ((($_FILES["link"]["type"] == "image/gif")
				|| ($_FILES["link"]["type"] == "image/jpeg")
				|| ($_FILES["link"]["type"] == "image/jpg")
				|| ($_FILES["link"]["type"] == "image/png")
				|| ($_FILES["link"]["type"] == "image/pjpeg"))
				)
				{
					
					if ($_FILES["link"]["error"] > 0)
					{
						echo "Return Code: " . $_FILES["link"]["error"] . "<br />";
					}
					else
					{
						
						/*$surfix_string = uniqid();
						//echo "<script>alert('".$_FILES["file_image"]["tmp_name"]."');</script>";
						@move_uploaded_file($_FILES["file_image"]["tmp_name"],
						$ha_absolute_path .$surfix_string."_".$_FILES["file_image"]["name"]);
						$image_name = $surfix_string."_".$_FILES["file_image"]["name"];*/
						
						$surfix_string = uniqid();
						//echo "<script>alert('".$_FILES["file_image"]["tmp_name"]."');</script>";
						@move_uploaded_file($_FILES["link"]["tmp_name"],
						$ha_absolute_path .$surfix_string."_".$_FILES["link"]["name"]);
						$link = $surfix_string."_".$_FILES["link"]["name"];
						
						
						if($count > 0)
						{
							$db =& JFactory::getDBO();
							$q = "UPDATE #__topbanner SET `link` = '$link' WHERE `id` = $id";
							$db->setQuery($q);
							$db->query();
						}else{
							$db =& JFactory::getDBO();
							$q = "INSERT INTO #__topbanner(`menuid`,`link`) VALUES('$id','$link')";
							$db->setQuery($q);
							$db->query();
						}
					}
				}
				else{
					echo "Invalid file";
				}
		}
	}
	
	
	
	
	go2com();
}
function go2com(){
	echo "<script>window.location='index.php?option=com_topbanner'</script>";
}